package org.light.reflecttable.facade;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.light.domain.Domain;
import org.light.domain.ManyToMany;
import org.light.domain.ManyToManyCandidate;
import org.light.domain.Project;
import org.light.facade.ExcelWizardFacade;
import org.light.reflecttable.service.TableService;
import org.light.reflecttable.serviceimpl.TableServiceImpl;
import org.light.reflecttable.utils.ReflectUtil;
import org.light.shiroauth.ShiroAuthModule;
import org.light.simpleauth.SimpleAuthModule;
import org.light.utils.StringUtil;

public class ReflectDbToProjectFacade extends HttpServlet{
	private static final long serialVersionUID = 4564381656126501278L;

	protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		try {
			String dbType = request.getParameter("dbType");
			String dbLink = request.getParameter("dbLink");
			String dbName = request.getParameter("dbName");
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			String technicalStack = request.getParameter("technicalStack");
			
			int topn = -1;
			if (StringUtil.isInteger(request.getParameter("topn"))) topn = Integer.parseInt(request.getParameter("topn"));
			
			TableServiceImpl.dbType = dbType;
			TableServiceImpl.dbLink = dbLink;
			TableServiceImpl.dbName = dbName;
			TableServiceImpl.userName = userName;
			TableServiceImpl.password = password;
			TableServiceImpl.topn = topn;
			
			Project p = new Project();
			p.setStandardName(StringUtil.capFirst(StringUtil.changeTableColumtoDomainField(TableServiceImpl.dbName)));
			p.setDbName(TableServiceImpl.dbName);
			p.setDbUsername(TableServiceImpl.userName);
			p.setDbPassword(TableServiceImpl.password);
			p.setDbType(TableServiceImpl.dbType);
			p.setExcelTemplateName(p.getStandardName()+".xls");
			
			TableService tbService = new TableServiceImpl();
			List<Domain> dbDomains = tbService.reflectDbToDomainList(TableServiceImpl.dbName,TableServiceImpl.dbType);
			List<ManyToManyCandidate> mtmcs = ReflectUtil.decorateMtmCandidatesFromRawDomains(dbDomains);
			List<List<ManyToManyCandidate>> dataMtmcs = ReflectUtil.getMtmCandidateValuesFromRawDomains(dbDomains);
			dbDomains = ReflectUtil.filterMtmCandidatesFromRawDomains(dbDomains);
			p.setDomains(dbDomains);
			p.setMtmCandidates(mtmcs);
			Domain sampleDomain = dbDomains.get(0);
			p.setPureDbPrefix(sampleDomain.getPureDbPrefix());
			List<List<Domain>> dataDomains = new ArrayList<>();
			for (Domain d:dbDomains) {
				dataDomains.add(d.getDummyDb());
			}
			p.setDataDomains(dataDomains);
			p.setMtmCandidatesValues(dataMtmcs);

			if ("jeeeu".equalsIgnoreCase(technicalStack) || "mjeeeu".equalsIgnoreCase(technicalStack)) {
				SimpleAuthModule sam = tbService.reflectSimpleAuthModule(dbDomains,dataDomains);
				if (sam != null) p.addModule(sam);
				ReflectUtil.decorateJEEEUProject(p);
			} else if ("simple".equalsIgnoreCase(technicalStack)) {
				SimpleAuthModule sam = tbService.reflectSimpleAuthModule(dbDomains,dataDomains);
				if (sam != null) p.addModule(sam);
				ReflectUtil.decorateSimpleProject(p);
			} else if ("tower".equalsIgnoreCase(technicalStack)) {
				SimpleAuthModule sam = tbService.reflectSimpleAuthModule(dbDomains,dataDomains);
				if (sam != null) p.addModule(sam);
			} else if ("sbmeu".equalsIgnoreCase(technicalStack) ||"smeu".equalsIgnoreCase(technicalStack) ||"msmeu".equalsIgnoreCase(technicalStack)||"boot3".equalsIgnoreCase(technicalStack)||"jpa".equalsIgnoreCase(technicalStack)){
				ShiroAuthModule sam = tbService.reflectShiroAuthModule(dbDomains,dataDomains);
				if (sam != null) p.addModule(sam);
				ReflectUtil.decorateJEEEUProject(p);
			}
			ExcelWizardFacade.setInstance(p);
			
			response.sendRedirect("./listServerProjectDomainsDataFacade");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public String getServletInfo(){
		return "Powered by Mind Rules.";
	}

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		processRequest(request, response);
	}

}
